package net.huashitong.scheduling.web;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.scheduling.entity.ProjectRecommendation;
import net.huashitong.scheduling.entity.ProjectReserve;
import net.huashitong.scheduling.entity.ProjectSecondRecom;
import net.huashitong.scheduling.entity.ProjectSecondRes;
import net.huashitong.scheduling.filter.ProjectReserveFilter;
import net.huashitong.scheduling.service.ProjectRecommendationService;
import net.huashitong.scheduling.service.ProjectReserveService;
import net.huashitong.scheduling.service.ProjectSecondRecomService;
import net.huashitong.scheduling.service.ProjectSecondResService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("app/secondProject")
public class ProjectRecommendationController extends JsonBaseController {

    @Autowired
    private ProjectRecommendationService projectRecommendationService;
    @Autowired
    private ProjectSecondRecomService projectSecondRecomService;
    @Autowired
    private ProjectSecondResService projectSecondResService;
    @Autowired
    private ProjectReserveService projectReserveService;

    public static final String[] months = {"12","11","10","09","08","07","06","05","04","03","02","01"};
    public static final String[] years={"2022", "2021","2020","2019","2018","2017","2016"};

    protected String getView(String arg0) {
        // TODO Auto-generated method stub
        return null;


    }

    /*
    项目推介二级界面
     */
    @ResponseBody
    @RequestMapping("/queryPRECOM")
    public Message queryPREC(@RequestBody ProjectReserveFilter filter) {
        Map<String, Object> maps = new HashMap<>();
        int ss=0;
        String date="";
        String month="";
        String year="";
        if(ObjectUtils.isBlank(filter.getDate())) {
            for (int i = 0; i < ProjectRecommendationController.years.length; i++) {
                if (ss == 1) {
                    break;
                }
                for (int j = 0; j < ProjectRecommendationController.months.length; j++) {
                    List<ProjectRecommendation> dateList = projectRecommendationService.queryByMonthAndYear(months[j],years[i]);
                    if (ObjectUtils.isNotBlank(dateList)) {
                        month=months[j];
                        year=years[i];
                        date=years[i]+"年"+months[j]+"月";
                        ss = 1;
                        break;

                    }

                }
            }

        }else{
            String months=filter.getDate().substring(4,6);
            String years=filter.getDate().substring(0,4);
            month=months;
            year=years;
            date=years+"年"+months+"月";

        }
        maps.put("date",date);
        filter.setMonth(month);
        filter.setYear(year);
        //按行业分类
        List<ProjectSecondRecom> industryList = projectSecondRecomService.queryByType("行业",filter);
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        List<Map<String, Object>> industrySecond = new ArrayList<>();
        List<Map<String, Object>> townSecond = new ArrayList<>();

        for (ProjectSecondRecom industry : industryList) {
            // “0”表示头部信息
            if ("0".equals(industry.getProjectId())) {
                if("重大项目推介".equals(industry.getTypeDetail())){
                    maps.put("totalCount",industry.getCount());
                }
                if("其他项目".equals(industry.getTypeDetail())){
                    maps.put("otherCount",industry.getCount());
                }
                if("工业服务业旅游".equals(industry.getTypeDetail())){
                    maps.put("industryAndServiceAndTour",industry.getCount());
                }
                if("10亿以上".equals(industry.getTypeDetail())){
                    maps.put("tenCount",industry.getCount());
                    maps.put("tenInvestment",industry.getProjectInvestment());
                    maps.put("unit","亿元");
                }
                if("5亿以上".equals(industry.getTypeDetail())){
                    maps.put("fiveCount",industry.getCount());
                    maps.put("fiveInvestment",industry.getProjectInvestment());
                    maps.put("unit","亿元");
                }
                if("总投资".equals(industry.getTypeDetail())){
                    maps.put("totalInvestment",industry.getProjectInvestment());
                    maps.put("unit","亿元");
                }

            }

            // “1”表示行业分类信息
            if ("1".equals(industry.getProjectId())) {
                Map<String, Object> map11 = new HashMap<>();
                map11.put("typeDetail", industry.getTypeDetail());
                map11.put("count", industry.getCount());

                map11.put("projectInvestment", industry.getProjectInvestment());
                map11.put("unit", "亿元");
                industrySecond.add(map11);

            }
            map1.put("data",industrySecond);
            map1.put("type","1");
            map1.put("title","按行业分布");

        }

        //按乡镇分类
        List<ProjectSecondRecom> townList = projectSecondRecomService.queryByType("乡镇",filter);
        for (ProjectSecondRecom town : townList) {
            Map<String, Object> map22 = new HashMap<>();
            map22.put("typeDetail", town.getTypeDetail());
            map22.put("count", town.getCount());
            map22.put("projectInvestment", town.getProjectInvestment());
            map22.put("unit", "亿元");
            townSecond.add(map22);
            map2.put("data",townSecond);
            map2.put("type","1");
            map2.put("title","按乡镇分布");


        }
        ;

        if(ObjectUtils.isNotBlank(industryList)){
            list.add(map1);
        }
        if(ObjectUtils.isNotBlank(townList)){
            list.add(map2);
        }
        maps.put("list", list);


        return coverMessage("200", "成功获取数据", maps);

    }

    /*
    项目储备二级界面
     */

    @ResponseBody
    @RequestMapping("/queryPRES")
    public Message queryPRES(@RequestBody ProjectReserveFilter filter) {
        Map<String, Object> maps = new HashMap<>();
        int ss=0;
        String date="";
        String month="";
        String year="";
        if(ObjectUtils.isBlank(filter.getDate())) {
            for (int i = 0; i < ProjectRecommendationController.years.length; i++) {
                if (ss == 1) {
                    break;
                }
                for (int j = 0; j < ProjectRecommendationController.months.length; j++) {
                    List<ProjectReserve> dateList = projectReserveService.queryByMonthAndYear(months[j], years[i]);
                    if (ObjectUtils.isNotBlank(dateList)) {
                        month=months[j];
                        year=years[i];
                        date=years[i]+"年"+months[j]+"月";
                        ss = 1;
                        break;

                    }

                }
            }


        }else{
            String months=filter.getDate().substring(4,6);
            String years=filter.getDate().substring(0,4);
            month=months;
            year=years;
            date=years+"年"+months+"月";



        }
        maps.put("date",date);
        filter.setMonth(month);
        filter.setYear(year);

        List<ProjectSecondRes> industryResList = projectSecondResService.queryByType("行业",filter);

        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        List<Map<String, Object>> industryResSecond = new ArrayList<>();
        List<Map<String, Object>> townResSecond = new ArrayList<>();
        for (ProjectSecondRes industry : industryResList) {
            // “0”表示头部信息
            if ("0".equals(industry.getProjectId())) {
                if("入库项目".equals(industry.getTypeDetail())){
                maps.put("totalCount",industry.getCount());
                }
                if("其他项目".equals(industry.getTypeDetail())){
                    maps.put("otherCount",industry.getCount());
                }
                if("工业服务业旅游业".equals(industry.getTypeDetail())){
                    maps.put("industryAndServiceAndTour",industry.getCount());
                }
                if("1亿元以上".equals(industry.getTypeDetail())){
                    maps.put("oneCount",industry.getCount());
                    maps.put("oneInvestment",industry.getProjectInvestment());
                    maps.put("unit","亿元");
                }
                if ("5亿元以上".equals(industry.getTypeDetail())){
                    maps.put("fiveCount",industry.getCount());
                    maps.put("fiveInvestment",industry.getProjectInvestment());
                    maps.put("unit","亿元");
                }
                if("总投资".equals(industry.getTypeDetail())){
                    maps.put("totalInvestment",industry.getProjectInvestment());
                    maps.put("unit","亿元");
                }


            }


            // “1”表示行业分类信息
            if ("1".equals(industry.getProjectId())) {
                Map<String, Object> map11 = new HashMap<>();
                map11.put("typeDetail", industry.getTypeDetail());
                map11.put("count", industry.getCount());
                map11.put("projectInvestment", industry.getProjectInvestment());
                map11.put("unit", "亿元");
                industryResSecond.add(map11);
            }
            map1.put("data",industryResSecond);
            map1.put("title","按行业分布");
            map1.put("type","1");


        }




        //按乡镇分类
        List<ProjectSecondRes> townResList = projectSecondResService.queryByType("乡镇",filter);
        for (ProjectSecondRes town : townResList) {
            Map<String, Object> map22 = new HashMap<>();
            map22.put("typeDetail",town.getTypeDetail());
            map22.put("score",town.getScore());
            map22.put("unit","分");
            townResSecond.add(map22);

            map2.put("data",townResSecond);
            map2.put("title","各乡镇项目储备综合得分");
            map2.put("type","2");
            map2.put("desc","含完成任务、工业服务业旅游业比例、开工出库比例三个指标，满分65分");


        }
        if(ObjectUtils.isNotBlank(industryResList)){
            list.add(map1);
        }
        if(ObjectUtils.isNotBlank(townResList)){
            list.add(map2);
        }





        maps.put("list",list);
        return coverMessage("200", "成功获取数据", maps);

    }

    /*
    项目储备的三级页面
     */
    @ResponseBody
    @RequestMapping("/queryPRESTHIRD")
    public Message queryPRESTHIRD (@RequestBody ProjectReserveFilter filter) {

        List<Map<String, Object>> list = new ArrayList<>();

       // List<ProjectReserve> industryList=projectReserveService.query();
        String month="";
        String year="";
        int ss=0;
        if(ObjectUtils.isBlank(filter.getDate())) {
            for (int i = 0; i < ProjectRecommendationController.years.length; i++) {
                if (ss == 1) {
                    break;
                }
                for (int j = 0; j < ProjectRecommendationController.months.length; j++) {
                    List<ProjectReserve> dateList = projectReserveService.queryByMonthAndYear(months[j], years[i]);
                    if (ObjectUtils.isNotBlank(dateList)) {
                       month=months[j];
                       year=years[i];
                        ss = 1;
                        break;

                    }

                }
            }
            filter.setMonth(month);
            filter.setYear(year);

        }else{
            String months=filter.getDate().substring(4,6);
            String years=filter.getDate().substring(0,4);
            filter.setMonth(months);
            filter.setYear(years);


        }

        List<ProjectReserve> industryList=projectReserveService.queryByMonthAndYearAndCategoryDetail(filter);
        for(ProjectReserve industry:industryList){
            Map<String, Object> map1=new HashMap<>();
            map1.put("projectName",industry.getProjectName());
            map1.put("totalInvestment",industry.getTotalInvestment());
            map1.put("unit","万元");
            map1.put("developmentArea",industry.getDevelopmentArea());
            map1.put("developmentYear",industry.getDevelopmentYear());
            map1.put("contact",industry.getContact());
            map1.put("phone",industry.getPhone());
            map1.put("developmentCompany",industry.getDevelopmentCompany());
            map1.put("developmentContent",industry.getDevelopmentContent());
            map1.put("contact",industry.getContact());

            list.add(map1);
            }




        return coverMessage("200", "成功获取数据", list);
    }

    /*
    项目推介的三级页面
     */
    @ResponseBody
    @RequestMapping("/queryPRECOMTHIRD")
    public Message queryPRECOMTHIRD (@RequestBody ProjectReserveFilter filter) {
        List<Map<String, Object>> list = new ArrayList<>();
        String month="";
        String year="";
        int ss=0;
        if(ObjectUtils.isBlank(filter.getDate())) {
            for (int i = 0; i < ProjectRecommendationController.years.length; i++) {
                if (ss == 1) {
                    break;
                }
                for (int j = 0; j < ProjectRecommendationController.months.length; j++) {
                    List<ProjectRecommendation> dateList = projectRecommendationService.queryByMonthAndYear(months[j], years[i]);
                    if (ObjectUtils.isNotBlank(dateList)) {
                        month=months[j];
                        year=years[i];
                        ss = 1;
                        break;

                    }

                }
            }
            filter.setMonth(month);
            filter.setYear(year);

        }else{
            String months=filter.getDate().substring(4,6);
            String years=filter.getDate().substring(0,4);
            filter.setMonth(months);
            filter.setYear(years);


        }
        List<ProjectRecommendation> industryList=projectRecommendationService.queryByMonthAndYearAndCategoryDetail(filter);
        for(ProjectRecommendation industry:industryList){
            Map<String, Object> map1=new HashMap<>();
            map1.put("projectName",industry.getProjectName());
            map1.put("totalInvestment",industry.getTotalInvestment());
            map1.put("developmentArea",industry.getDevelopmentArea());
            map1.put("developmentYear",industry.getFrontCompany());
            map1.put("developmentContent",industry.getDevelopmentContent());
            map1.put("developmentCompany",industry.getFrontSituation());
            map1.put("unit","亿元");
            map1.put("contact",industry.getContact());
            map1.put("phone",industry.getPhone());
            list.add(map1);
        }



        return coverMessage("200", "成功获取数据", list);
    }
}